<?php


namespace app\admin\services\task;


use app\admin\model\CheckWorkFace;
use app\admin\model\Distributor;
use app\admin\model\EquIp;
use app\admin\model\ShopCheckWork;
use app\admin\model\ShopCheckWorkWarning;

class CheckWorkWarningService
{

    //初始化分流数据
    public static function initialization($data)
    {
        //查出当前所有店铺的考勤信息
        $shops = Distributor::field('a.id as shop_id,a.check_work_id,a.encrypt,a.name,b.*')
            ->alias('a')
            ->leftJoin('lg_check_work_attendance b','a.check_work_id = b.id')
            ->where('a.status',1)
            ->where('b.status',2)
            ->whereNotNull('check_work_id')
            ->select();
        foreach ($shops as $key => $value){
            //验证设备是否正常运行
            $check_equip = EquIp::check_shop_equip($value['shop_id']) ;
            if(!$check_equip){
                continue;
            }
            self::check_go_work($value);
            self::check_off_work($value);
        }
    }

    //验证今天是否已经预警完成
    public static function check_shop_work($data,$type)
    {
        $date = date('Y-m-d');
        $where = array(
            'shop_id' => $data['shop_id'] ,
            'date' => $date ,
            'type' => $type
        );
        $result = ShopCheckWorkWarning::where($where)->find();
        return $result;
    }

    //验证今天是否已经上下班了
    public static function check_work($data,$type)
    {
        $date = date('Y-m-d');
        $where = array(
            'shop_id' => $data['shop_id'] ,
            'punch_date' => $date ,
            'type' => $type
        );
        $result = CheckWorkFace::where($where)
//            ->fetchSql(true)
            ->find();
        return $result;
    }

    //上班时间无考勤预警
    public static function check_go_work($data)
    {
        $time = strtotime($data['go_work_time']);
        if (time() > $time){
            $face = self::check_shop_work($data,1);
            if($face){
                return false;
            }
            $check_work = self::check_work($data,1);
            if(!$check_work){
                $warning_data = array(
                    'shop_id' => $data['shop_id'] ,
                    'inspection_time' => time() ,
                    'date' => date('Y-m-d') ,
                    'status' => 2 ,
                    'type' => 1 ,
                    'remarks' => '未读取到考勤记录' ,
                    'create_time' => time() ,
                    'update_time' => time() ,
                );
                ShopCheckWorkWarning::create($warning_data);
            }
            return true;
        }
        return true;
    }

    //下班时间无考勤预警
    public static function check_off_work($data)
    {
        $time = strtotime($data['off_work_time']);
        if (time()  > $time + 3600 ){
            $face = self::check_shop_work($data,2);
            if($face){
                return false;
            }
            $check_work = self::check_work($data,2);
            if(!$check_work){
                $warning_data = array(
                    'shop_id' => $data['shop_id'] ,
                    'inspection_time' => time() ,
                    'date' => date('Y-m-d') ,
                    'status' => 2 ,
                    'type' => 2 ,
                    'remarks' => '未读取到考勤记录' ,
                    'create_time' => time() ,
                    'update_time' => time() ,
                );
                ShopCheckWorkWarning::create($warning_data);
            }
            return true;
        }
        return true;
    }

}